package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.device;

import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.EntityTypeParamerMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Dic;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EntityTypeParamer;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.device.JsmEntityTypeParamerService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.dic.JsmDicService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huangxueqian on 2017/4/28/028.
 */
public class JsmEntityTypeParamerServiceImpl implements JsmEntityTypeParamerService {
    @Autowired
    private EntityTypeParamerMapper entityTypeParamerMapper;
    @Autowired
    private JsmDicService jsmDicService;

    @Override
    public List<EntityTypeParamer> getEntityTypeParamersByEtId(String entityTypeId) throws Exception {
        List<EntityTypeParamer> lists = entityTypeParamerMapper.getEntityTypeParamersByEtId(entityTypeId);
        return offsetEntityTypeParamer(lists);
    }

    /**
     * @param lists
     */
    private List<EntityTypeParamer> offsetEntityTypeParamer(List<EntityTypeParamer> lists) {
        List<EntityTypeParamer> entityTypeParamers = new ArrayList<>();
        if (lists != null && lists.size() > 0) {
            for (EntityTypeParamer entityTypeParamer : lists) {
                if (StringUtils.isNotEmpty(entityTypeParamer.getDataSourceFromName()) || StringUtils.isNotEmpty(entityTypeParamer.getDataSourceFrom())) {
                    Dic dic = new Dic();
                    if(StringUtils.isNotEmpty(entityTypeParamer.getDataSourceFrom())){
                        dic.setDicTableName(entityTypeParamer.getDataSourceFrom());
                    }
                    if(StringUtils.isNotEmpty(entityTypeParamer.getDataSourceFromName())){
                        dic.setDicType(entityTypeParamer.getDataSourceFromName());
                    }
                    try {
                        List<Dic> dicList = jsmDicService.getDicsByType(dic);
                        if (dicList != null && dicList.size() > 0) {
                            entityTypeParamer.setDicList(dicList);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                entityTypeParamers.add(entityTypeParamer);
            }
        }
        return entityTypeParamers;
    }
}
